﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TvServer.Epg
{
    public delegate void GenerateXmlTvEventHandler(XmlTv e);


    public class XmlTv : XmlDocument
    {
        public int ChannelCount
        {
            get { return SelectNodes("tv/channel").Count; }
        }

        public int ProgrammeCount
        {
            get { return SelectNodes("tv/programme").Count; }
        }

        public XmlTv()
        {
            XmlDeclaration xmlDeclaration = CreateXmlDeclaration("1.0", "ISO-8859-1", null);
            AppendChild(xmlDeclaration);

            XmlDocumentType xmlDocumentType = CreateDocumentType("tv", null, "xmltv.dtd", null);
            AppendChild(xmlDocumentType);

            XmlElement xmlTvElement = CreateElement("tv");
            AppendChild(xmlTvElement);
        }

        private void AddChannnelElement(Channel channel)
        {
            XmlElement xmlChannelElement = CreateElement("channel");

            XmlAttribute xmlIdAttribute = CreateAttribute("id");
            xmlIdAttribute.Value = channel.SourceUrl;
            xmlChannelElement.Attributes.Append(xmlIdAttribute);

            XmlElement xmlDisplayNameElement = CreateElement("display-name");
            xmlDisplayNameElement.InnerText = channel.Name;
            xmlChannelElement.AppendChild(xmlDisplayNameElement);

            //XmlElement xmlIconElement = CreateElement("icon");
            //xmlChannelElement.AppendChild(xmlIconElement);

            //XmlAttribute xmlSrcAttribute = CreateAttribute("src");
            //xmlIconElement.Attributes.Append(xmlSrcAttribute);

            XmlNode xmlFirstProgrammeNode = SelectSingleNode("tv/programme[1]");
            if (xmlFirstProgrammeNode != null)
                DocumentElement.InsertBefore(xmlChannelElement, xmlFirstProgrammeNode);
        }

        public void AddProgrammeElement(Schedule schedule)
        {
            XmlElement xmlProgrammeElement = CreateElement("programme");

            XmlAttribute xmlProgrammeStartAttribute = CreateAttribute("start");
            xmlProgrammeStartAttribute.Value = schedule.PgStart.ToString();
            xmlProgrammeElement.Attributes.Append(xmlProgrammeStartAttribute);

            XmlAttribute xmlProgrammeStopAttribute = CreateAttribute("stop");
            xmlProgrammeStopAttribute.Value = schedule.PgStop.ToString();
            xmlProgrammeElement.Attributes.Append(xmlProgrammeStopAttribute);

            XmlAttribute xmlProgrammeChannelAttribute = CreateAttribute("channel");
            xmlProgrammeChannelAttribute.Value = schedule.Channel.SourceUrl;
            xmlProgrammeElement.Attributes.Append(xmlProgrammeChannelAttribute);

            XmlElement xmlProgrammeTitleElement = CreateElement("title");
            xmlProgrammeTitleElement.InnerText = schedule.ProTitle;
            xmlProgrammeElement.AppendChild(xmlProgrammeTitleElement);

            XmlAttribute xmlProgrammeTitleLangAttribute = CreateAttribute("lang");
            xmlProgrammeTitleLangAttribute.Value = "da";
            xmlProgrammeTitleElement.Attributes.Append(xmlProgrammeTitleLangAttribute);

            if (schedule.PpuPunchline != null)
            {
                XmlElement xmlProgrammeSubTitleElement = CreateElement("sub-title");
                xmlProgrammeSubTitleElement.InnerText = schedule.PpuPunchline;
                xmlProgrammeElement.AppendChild(xmlProgrammeSubTitleElement);

                XmlAttribute xmlProgrammeSubTitleLangAttribute = CreateAttribute("lang");
                xmlProgrammeSubTitleLangAttribute.Value = "da";
                xmlProgrammeSubTitleElement.Attributes.Append(xmlProgrammeSubTitleLangAttribute);
            }
            if (schedule.PpuDescription != null)
            {
                XmlElement xmlProgrammeDescElement = CreateElement("desc");
                xmlProgrammeDescElement.InnerText = schedule.PpuDescription;
                xmlProgrammeElement.AppendChild(xmlProgrammeDescElement);

                XmlAttribute xmlProgrammeDescLangAttribute = CreateAttribute("lang");
                xmlProgrammeDescLangAttribute.Value = "da";
                xmlProgrammeDescElement.Attributes.Append(xmlProgrammeDescLangAttribute);
            }

            XmlNode xmlTvNode = SelectSingleNode("tv");
            if (xmlTvNode != null)
                DocumentElement.AppendChild(xmlProgrammeElement);

            // Add channel if it's not allready there
            XmlNode xmlChannelNode = SelectSingleNode("tv/channel[@id='" + schedule.Channel.SourceUrl + "']");
            if (xmlChannelNode == null)
                AddChannnelElement(schedule.Channel);
        }
    }
}
